﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Tsingda.CacheModule
{
    /// <summary>
    /// 以静态化的方式提供缓存服务
    /// </summary>
    public class StaticCache : ICache
    {
        internal static string RootPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "CacheFile" + "\\";
        #region Constructors
        public StaticCache(string folder)
        {
            StringRootPath = folder;
        }
        public StaticCache()
            : this(RootPath)
        {

        }
        #endregion
        /// <summary>
        /// 文件存放的根目录,以 \ 结束
        /// </summary>
        public string StringRootPath { get; set; }

        /// <summary>
        /// 缓存文件有有效期，创建后经过指定分钟后失效
        /// </summary>
        private double dMinutes = 30;

        #region 接口方法
        public bool Container(string key)
        {
            if (string.IsNullOrEmpty(key))
                return false;
            else
            {
                if (System.IO.File.Exists(KeyToFilePath(key)))//文件存在
                {
                    if (DateTime.Now.Subtract(System.IO.File.GetLastWriteTime(KeyToFilePath(key))).TotalMinutes < dMinutes)//文件未过期
                        return true;
                    else
                        return false;
                }
                else
                    return false;

            }
        }

        public void Add(string key, object memo, int minutes)
        {
            if (!System.IO.Directory.Exists(KeytoDirectoryPath(key)))
                System.IO.Directory.CreateDirectory(KeytoDirectoryPath(key));

            using (StreamWriter write = new StreamWriter(KeyToFilePath(key), false, System.Text.Encoding.UTF8))
            {
                write.Write(memo);
                write.Write("<!-- From StaticCache{" + key + "} -->");
            }
        }


        /// <summary>
        /// 清除缓存中指字键值的项
        /// </summary>
        /// <param name="key">键值</param>
        public void Remove(string key)
        {
            if (System.IO.File.Exists(KeyToFilePath(key)))
            {
                System.IO.File.Delete(KeyToFilePath(key));
            }
        }

        /// <summary>
        /// 根据Key值获取缓存中内容,若不存在,则返回默认字符串 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Get(string key)
        {
            return null;
        }
        #endregion


        public void Write(System.Web.HttpApplication application, string key)
        {
            application.Context.Response.WriteFile(KeyToFilePath(key));
        }

        /// <summary>
        /// 通过key推导缓存文件存放目录信息
        /// </summary>
        /// <param name="stringKey"></param>
        /// <returns></returns>
        private string KeytoDirectoryPath(string stringKey)
        {
            string stringDirectoryPath = StringRootPath;
            if (!string.IsNullOrEmpty(stringKey))
            {
                stringDirectoryPath += stringKey.Substring(0, 1) + "\\" + stringKey.Substring(stringKey.Length - 1, 1) + "\\";
            }
            return stringDirectoryPath;
        }

        /// <summary>
        /// 通过key推导缓存文件存放路径
        /// </summary>
        /// <param name="stringKey"></param>
        /// <returns></returns>
        private string KeyToFilePath(string stringKey)
        {
            return KeytoDirectoryPath(stringKey) + stringKey + ".html";
        }


    }
}
